package pku;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class MyObjectOutputStream extends ObjectOutputStream {
	
	private static File f;
	
	public static MyObjectOutputStream newInstance(File file, OutputStream out) throws IOException {
	     f = file;
	    return new MyObjectOutputStream(out);
	}//通过方法初始化f,获取对象。
	 
	private MyObjectOutputStream(OutputStream out) throws IOException {
	     super(out);//会调用重写的writeStreamHeader()方法
	}//不能在类的外部使用new,但在类的内部可以
	 
    @Override
    protected void writeStreamHeader() throws IOException {
        if (!f.exists() || (f.exists() && f.length() == 0)) {
             super.writeStreamHeader();
        } else {
              super.reset();
        }
    }
}
